﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.DXErrorProvider;
using DevExpress.XtraEditors.Controls;

namespace QuanLyThuNgan
{
    public partial class frm_DM_GiaPhauThuat : DevExpress.XtraEditors.XtraForm
    {
        public frm_DM_GiaPhauThuat()
        {
            InitializeComponent();
        }

        #region "enum"
        enum DatasetStatus
        {
            Add=1,
            Edit=2,
            None=0
        }
        #endregion

        string sTenSearch = "";
        string sColSearch;

        public static string sIDSearch="" ;
        public static string sTenValueSearch="";
        string sTenTam, sColTam;

        int iRowIndex;

        private DatasetStatus m_Status = DatasetStatus.None;
        GiaPhauThuatCTL giaPTCtl = new GiaPhauThuatCTL();
        GiaPhauThuatDTO giaPTDto = new GiaPhauThuatDTO();

        private void frm_DM_GiaPhauThuat_Load(object sender, EventArgs e)
        {
            //load loai pt-------------------------------------
            DataTable dtLoadPhanLoaiPT = giaPTCtl.m_SelectPhanLoaiPT();
            ClassHelp.m_gridLookUpEdit(dtLoadPhanLoaiPT, gcboPhanLoaiPT, "TenDMPhanLoaiPT", "idphanloaipt");
            //gcboPhanLoaiPT.Properties.View.Columns["idphanloaipt"].Visible = false;
            //load phan loai ptbhyt

            DataTable dtLoadPhanLoaiPTBHYT = giaPTCtl.m_SelectPhanLoaiPTBHYT();
            ClassHelp.m_gridLookUpEdit(dtLoadPhanLoaiPTBHYT, gcboPhanLoaiPTBHYT, "TenPhanLoaiPTBHYT", "IDPhanLoaiPTBHYT");
          //  gcboPhanLoaiPTBHYT.Properties.View.Columns["IDPhanLoaiPTBHYT"].Visible = false;
            //LOAD LOAI PT-------------------------------------------------------
            DataTable dtLoadLoaiPT = giaPTCtl.m_SelectLoaiPT();
            ClassHelp.m_gridLookUpEdit(dtLoadLoaiPT, gcboLoaiPT, "TenLoaiPT", "loai");
           // gcboLoaiPT.Properties.View.Columns["loai"].Visible = false;
            //load danh sach PT
            DataTable dtloadGiaPT = giaPTCtl.m_SelectAllGiaPT();
            gdGiaPT.DataSource = dtloadGiaPT;

            //set button condition------------------------
            setReadOnly();
            btnInsert.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
            btnSave.Enabled = false;
        }

        //check value insert-------------------------------------
        bool m_checkSave()
        {
            bool bCheckValue = true;
            if (Uit.it_Parse.ToString(gcboLoaiPT.EditValue) == "-1" || Uit.it_Parse.ToString(gcboLoaiPT.EditValue) == "")
            {
                dxErrorProvider1.SetError(gcboLoaiPT, "Phải nhập loại phẫu thuật");
                //gcboLoaiPT.Focus();
                bCheckValue = false;
            }

            if (txtTenGiaPT.Text.ToString() == "")
            {
                dxErrorProvider1.SetError(txtTenGiaPT, "Phải nhập Tên phẫu thuật thủ thuật");
                txtTenGiaPT.Focus();
                bCheckValue = false;
            }


            if (Uit.it_Parse.ToString(gcboPhanLoaiPT.EditValue) == "-1" || Uit.it_Parse.ToString(gcboPhanLoaiPT.EditValue) == "")
            {
                dxErrorProvider1.SetError(gcboPhanLoaiPT, "Phải nhập Phân loại phẫu thuật");
                //gcboPhanLoaiPT.Focus();
                bCheckValue = false;
            
            }

            if (Uit.it_Parse.ToString(gcboPhanLoaiPTBHYT.EditValue) == "-1" || Uit.it_Parse.ToString(gcboPhanLoaiPTBHYT.EditValue) == "")
            {
                dxErrorProvider1.SetError(gcboPhanLoaiPTBHYT, "Phải nhập Phân loại phẫu thuật BHYT");
                //gcboPhanLoaiPTBHYT.Focus();
                bCheckValue = false;
            
            }

            if (txtDGPT.Text.ToString() == "")
            {
                dxErrorProvider1.SetError(txtDGPT, "Phải nhập Đơn giá phẫu thuật");
                txtDGPT.Focus();
                bCheckValue = false;
            }

            if (txtGiaBHYT.Text.ToString() == "")
            {
                dxErrorProvider1.SetError(txtGiaBHYT, "Phải nhập Đơn giá BHYT");
                txtGiaBHYT.Focus();
                bCheckValue = false;
            }

            //set trung ten gia phau thuat------------------------------
            for (int i = 0; i < gvGiaPT.RowCount; i++)
            {
                if (i != iRowIndex)
                {
                    DataRow drpt = gvGiaPT.GetDataRow(i);
                    if (drpt["TenGiaPT"].ToString().ToUpper().Trim() == txtTenGiaPT.Text.ToString().ToUpper().Trim())
                    {

                        //XtraMessageBox.Show("Trùng tên giá phẫu thuật");
                        dxErrorProvider1.SetError(txtTenGiaPT, "Trùng tên phẫu thuật");
                        txtTenGiaPT.Focus();
                        bCheckValue = false;
                    }
                    else
                        if (txtTenGiaPT.Text.ToString() == "")
                        {
                            break;
                        }
                }               
            }
            return bCheckValue;
        }

        //luu thong tin du lieu
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (m_checkSave() == true)
            {
                giaPTDto.SMaLoaiPT = gcboLoaiPT.EditValue.ToString();
                giaPTDto.STenGiaPT = txtTenGiaPT.Text.ToString().Trim();
                giaPTDto.Schidanh = txtTenGiaPT.Text.ToString().Trim();
                giaPTDto.Iidphanloaipt = Uit.it_Parse.ToInteger(gcboPhanLoaiPT.EditValue.ToString());
                giaPTDto.IIDPhanLoaiPTBHYT = Uit.it_Parse.ToInteger(gcboPhanLoaiPTBHYT.EditValue.ToString());
                giaPTDto.Idongia = Uit.it_Parse.ToInteger(txtDGPT.Text.ToString().Trim());
                giaPTDto.Smabhyt = txtMaBHYT.Text.ToString().Trim();
                giaPTDto.Igiabhyt = Uit.it_Parse.ToInteger(txtGiaBHYT.Text.ToString().Trim());
                giaPTDto.Bstopsd = Uit.it_Parse.ToBoolean(chkStop.Checked);
                DataRow dr = gvGiaPT.GetDataRow(gvGiaPT.FocusedRowHandle);
                if (m_Status == DatasetStatus.Add)
                {
                    giaPTCtl.m_InsertGiaPT(giaPTDto);
                    XtraMessageBox.Show("Thêm giá Phẩu Thuật thành công");
                }
                else
                {
                    if (m_Status == DatasetStatus.Edit)
                    {
                        giaPTDto.Smaso = dr["maso"].ToString();
                        giaPTCtl.m_UpdateGiaPT(giaPTDto);
                        XtraMessageBox.Show("Cập nhật giá Phẩu Thuật thành công");
                    }
                }
            }
            else
            {
                XtraMessageBox.Show("Phải hoàn thành thông tin");
                return;
            }

            //XtraMessageBox.Show("Hoàn thành thông tin");
            //load danh sach PT
            DataTable dtloadGiaPT = giaPTCtl.m_SelectAllGiaPT();
            gdGiaPT.DataSource = dtloadGiaPT;

            btnSave.Enabled = false;
            btnUpdate.Enabled = true;
            btnInsert.Enabled = true;
            setReadOnly();
            setNullValue();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            m_Status = DatasetStatus.Add;

            btnSave.Enabled = true;
            btnInsert.Enabled = false;
            setFalseReadOnly();
            setNullValue();
            //reset load du lieu----------------------
            gvGiaPT.ActiveFilterEnabled = false;
            gvGiaPT.ClearColumnsFilter();
            gvGiaPT.OptionsFilter.Reset();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            m_Status = DatasetStatus.Edit;

            btnSave.Enabled = true;
            btnInsert.Enabled = true;
            btnUpdate.Enabled = false;
            m_setFalseReadOnly();
        }

        private void gvGiaPT_DoubleClick(object sender, EventArgs e)
        {
            gdGiaPT.Focus();
            DataRow drpt = gvGiaPT.GetDataRow(gvGiaPT.FocusedRowHandle);
            gcboLoaiPT.EditValue = drpt["loai"].ToString();
            txtTenGiaPT.Text = drpt["TenGiaPT"].ToString();
            gcboPhanLoaiPT.EditValue = drpt["idphanloaipt"].ToString();
            gcboPhanLoaiPTBHYT.EditValue = drpt["IDPhanLoaiPTBHYT"].ToString();
            txtDGPT.Text = drpt["dongia"].ToString();
            txtMaBHYT.Text = drpt["mabhyt"].ToString();
            txtGiaBHYT.Text = drpt["giabhyt"].ToString();
            chkStop.Checked = Uit.it_Parse.ToBoolean(drpt["stopsd"].ToString());

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DataRow drpt = gvGiaPT.GetDataRow(gvGiaPT.FocusedRowHandle);    
            if (drpt != null)
            {
                giaPTDto.Smaso = drpt["maso"].ToString();
                giaPTCtl.m_DeleteGiaPT(giaPTDto);

                //load danh sach PT
                DataTable dtloadGiaPT = giaPTCtl.m_SelectAllGiaPT();
                gdGiaPT.DataSource = dtloadGiaPT;
            }
            XtraMessageBox.Show("Xóa thành công", "Thông báo");
            setNullValue();
            //reset load du lieu----------------------
            gvGiaPT.ActiveFilterEnabled = false;
            gvGiaPT.ClearColumnsFilter();
            gvGiaPT.OptionsFilter.Reset();
        }

        #region "SetCondition"
        void setReadOnly()
        {

            gcboLoaiPT.Properties.ReadOnly = true;
            txtTenGiaPT.Properties.ReadOnly = true;
            gcboPhanLoaiPT.Properties.ReadOnly = true;
            gcboPhanLoaiPTBHYT.Properties.ReadOnly = true;
            txtDGPT.Properties.ReadOnly = true;
            txtMaBHYT.Properties.ReadOnly = true;
            txtGiaBHYT.Properties.ReadOnly = true;
            chkStop.Properties.ReadOnly = true;
        }

        void setFalseReadOnly()
        {
            gcboLoaiPT.Properties.ReadOnly = false;
            txtTenGiaPT.Properties.ReadOnly = false;
            gcboPhanLoaiPT.Properties.ReadOnly = false;
            gcboPhanLoaiPTBHYT.Properties.ReadOnly = false;
            txtDGPT.Properties.ReadOnly = false;
            txtMaBHYT.Properties.ReadOnly = false;
            txtGiaBHYT.Properties.ReadOnly = false;
            chkStop.Properties.ReadOnly = false;
        }

        void m_setFalseReadOnly()
        {
            txtTenGiaPT.Properties.ReadOnly = false;
            gcboPhanLoaiPT.Properties.ReadOnly = false;
            gcboPhanLoaiPTBHYT.Properties.ReadOnly = false;
            txtDGPT.Properties.ReadOnly = false;
            txtMaBHYT.Properties.ReadOnly = false;
            txtGiaBHYT.Properties.ReadOnly = false;
            chkStop.Properties.ReadOnly = false;
        }

        void setNullValue()
        {
            gcboLoaiPT.EditValue = -1;
            txtTenGiaPT.Text = "";
            gcboPhanLoaiPT.EditValue = -1;
            gcboPhanLoaiPTBHYT.EditValue = -1;
            txtDGPT.Text = "";
            txtMaBHYT.Text = "";
            txtGiaBHYT.Text = "";
            chkStop.Checked = false;
        }
        #endregion

        #region "Validation"

        private void txtTenGiaPT_KeyPress(object sender, KeyPressEventArgs e)
        {
            //e.KeyChar = char.Parse(e.KeyChar.ToString().ToUpper());
        }

        private void txtMaBHYT_KeyPress(object sender, KeyPressEventArgs e)
        {
            //e.KeyChar = char.Parse(e.KeyChar.ToString().ToUpper());
        }

        private void txtDGPT_KeyPress(object sender, KeyPressEventArgs e)
        {
           
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                XtraMessageBox.Show("Chỉ nhập kí tự số và không được nhập ký tự chữ cái");
                e.Handled = true;
            }
            
        }

        private void txtGiaBHYT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                XtraMessageBox.Show("Chỉ nhập kí tự số và không được nhập ký tự chữ cái");
                e.Handled = true;
            }
        }

        private void txtTenGiaPT_Leave(object sender, EventArgs e)
        {
            
            //for (int i = 0; i < gvGiaPT.RowCount; i++)
            //{
            //    DataRow drpt = gvGiaPT.GetDataRow(i);
            //    if (drpt["TenGiaPT"].ToString().ToUpper().Trim() == txtTenGiaPT.Text.ToString().Trim() || drpt["TenGiaPT"].ToString().ToLower().Trim() == txtTenGiaPT.Text.ToString().Trim())
            //    {

            //        XtraMessageBox.Show("Trùng tên giá phẫu thuật");
            //        dxErrorProvider1.SetError(txtTenGiaPT, "Trùng tên phẫu thuật");
            //        txtTenGiaPT.Focus();
            //        return;
            //    }
            //    else
            //        if (txtTenGiaPT.Text.ToString() == "")
            //        {
            //            break;
            //        }
                    
            //}
            
        }

        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnInsert.Enabled = true;
            btnSave.Enabled = false;
            setNullValue();
            setReadOnly();
            DataTable dtloadGiaPT = giaPTCtl.m_SelectAllGiaPT();
            gdGiaPT.DataSource = dtloadGiaPT;

            //reset load du lieu----------------------
            gvGiaPT.ActiveFilterEnabled = false;
            gvGiaPT.ClearColumnsFilter();
            gvGiaPT.OptionsFilter.Reset();
        }
        //tim ten can search---------------------------------------------
        private void gvGiaPT_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            
            sTenSearch = e.Value.ToString();
            sColSearch = e.Column.FieldName;
        }
        //set ten can tim----------------------------------------------------------------
        private void gvGiaPT_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gvGiaPT;
            view.ActiveFilter.Add(view.Columns[sColSearch], new ColumnFilterInfo("[" + sColSearch + "] Like '%" + sTenSearch + "%'", ""));
        
        }


        //set tab value and get value
        private void gcboPhanLoaiPT_Enter(object sender, EventArgs e)
        {
            SendKeys.Send("{F4}");
        }

        private void gcboPhanLoaiPTBHYT_Enter(object sender, EventArgs e)
        {
            SendKeys.Send("{F4}");
        }

        //search loai phau thuat--------------------------------------------
        private void gcboLoaiPT_EditValueChanged(object sender, EventArgs e)
        {
            
            if (gcboLoaiPT.EditValue != null)
            {
                sIDSearch = gcboLoaiPT.EditValue.ToString();
                sTenValueSearch = gcboLoaiPT.Text;
                dxErrorProvider1.SetError(gcboLoaiPT, "");
            }
        }

        private void gridView3_CellValueChanging(object sender, CellValueChangedEventArgs e)
        {
            sTenTam = e.Value.ToString();
            sColTam = e.Column.FieldName;
        }

        private void gridView3_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gcboLoaiPT.Properties.View;
            view.ActiveFilter.Add(view.Columns[sColTam],
              new ColumnFilterInfo("[" + sColTam + "] Like '%" + sTenTam + "%'", ""));
        }

        //search phan loai phau thuat---------------------------------------------
        private void gcboPhanLoaiPT_EditValueChanged(object sender, EventArgs e)
        {
            if (gcboPhanLoaiPT.EditValue != null)
            {
                sIDSearch = gcboPhanLoaiPT.EditValue.ToString();
                sTenValueSearch = gcboPhanLoaiPT.Text;
                dxErrorProvider1.SetError(gcboPhanLoaiPT, "");
            }
            
        }

        private void gridLookUpEdit1View_CellValueChanging(object sender, CellValueChangedEventArgs e)
        {
            sTenTam = e.Value.ToString();
            sColTam = e.Column.FieldName;
        }

        private void gridLookUpEdit1View_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gcboPhanLoaiPT.Properties.View;
            view.ActiveFilter.Add(view.Columns[sColTam],
              new ColumnFilterInfo("[" + sColTam + "] Like '%" + sTenTam + "%'", ""));
        }

        //search phan loai PTBHYT------------------------------------------------------
        private void gcboPhanLoaiPTBHYT_EditValueChanged(object sender, EventArgs e)
        {
            if (gcboPhanLoaiPTBHYT.EditValue != null)
            {
                sIDSearch = gcboPhanLoaiPTBHYT.EditValue.ToString();
                sTenValueSearch = gcboPhanLoaiPTBHYT.Text;
                dxErrorProvider1.SetError(gcboPhanLoaiPTBHYT, "");
            }
            
        }

        private void gridView2_CellValueChanging(object sender, CellValueChangedEventArgs e)
        {
            sTenTam = e.Value.ToString();
            sColTam = e.Column.FieldName;
        }

        private void gridView2_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gcboPhanLoaiPTBHYT.Properties.View;
            view.ActiveFilter.Add(view.Columns[sColTam],
              new ColumnFilterInfo("[" + sColTam + "] Like '%" + sTenTam + "%'", ""));
        }

        //set value changing-----------------------------------------------
        private void txtTenGiaPT_EditValueChanged(object sender, EventArgs e)
        {
            if (txtTenGiaPT.Text.ToString() != "")
            {
                dxErrorProvider1.SetError(txtTenGiaPT, "");
            }
        }

        
        private void txtDGPT_EditValueChanged(object sender, EventArgs e)
        {
            if (txtDGPT.Text.ToString() != "")
            {
                dxErrorProvider1.SetError(txtDGPT, "");
            }
        }

        private void txtGiaBHYT_EditValueChanged(object sender, EventArgs e)
        {
            if (txtGiaBHYT.Text.ToString() != "")
            {
                dxErrorProvider1.SetError(txtGiaBHYT, "");
            }
        }

        private void gvGiaPT_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            iRowIndex = e.FocusedRowHandle;

            gcboLoaiPT.EditValue = (sender as GridView).GetFocusedRowCellValue("loai");
            txtTenGiaPT.EditValue = (sender as GridView).GetFocusedRowCellValue("TenGiaPT");
            gcboPhanLoaiPT.EditValue = (sender as GridView).GetFocusedRowCellValue("idphanloaipt");
            gcboPhanLoaiPTBHYT.EditValue = (sender as GridView).GetFocusedRowCellValue("IDPhanLoaiPTBHYT");
            txtDGPT.EditValue = (sender as GridView).GetFocusedRowCellValue("dongia");
            txtMaBHYT.EditValue = (sender as GridView).GetFocusedRowCellValue("mabhyt");
            txtGiaBHYT.EditValue = (sender as GridView).GetFocusedRowCellValue("giabhyt");
            chkStop.EditValue = (sender as GridView).GetFocusedRowCellValue("stopsd");
        }

        
       
    }
}